Skip to content

Python: omit failed Foundry turns from conversation chat history - #7637

Open
Shikhar Goel (sgoel2be24-cyber) wants to merge 5 commits into
microsoft:mainfrom
sgoel2be24-cyber:fix/foundry-skip-failed-conversation-persist
Open

Python: omit failed Foundry turns from conversation chat history#7637
Shikhar Goel (sgoel2be24-cyber) wants to merge 5 commits into
microsoft:mainfrom
sgoel2be24-cyber:fix/foundry-skip-failed-conversation-persist

Conversation

@sgoel2be24-cyber

@sgoel2be24-cyber Shikhar Goel (sgoel2be24-cyber) commented Aug 13, 2026

Copy link
Copy Markdown

Motivation & Context

When a hosted Foundry agent is used with conversation_id, a failed turn still stored its input items on the agentserver response/conversation store. The next request on that conversation replayed them via get_history(). Azure OpenAI does not keep failed input on the conversation.

Fixes #7630

Description & Review Guide

  • Let ResponsesAgentServerHost resolve and validate its configured/default response store before wrapping it, preserving hosted and local persistence defaults plus the resilient-background guard.
  • For synchronous non-background requests, determine the terminal result before the provider's initial create so failed input items never enter conversation history.
  • Keep existing response updates atomic at the provider level instead of using delete-then-create.
  • Reverted the earlier “skip MAF session persist on failure” change; that was the wrong store.
  • Tests in python/packages/foundry_hosting/tests/test_responses.py cover the HTTP conversation repro, provider behavior, default persistent storage, and the resilience guard.

Please focus on: failed synchronous turns stay out of get_history(), successful turns still persist, non-conversation runs are unchanged, and the base host's storage behavior is preserved.

Related Issue

Fixes #7630

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change.

Assistance: drafted with an AI coding agent and reviewed before opening.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Aug 13, 2026
@sgoel2be24-cyber

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@cecheta

Copy link
Copy Markdown
Member

Unfortunately this doesn't fix the issue, as the problem is that the messages get saved to the chat history, not the session.

@sgoel2be24-cyber

Copy link
Copy Markdown
Author

You're right — the first commit skipped the wrong store.

The agentserver response provider was persisting input items for failed turns, so the next request on the same conversation replayed them via chat history (get_history()), not the MAF session.

24ebe8b wraps the response store so failed turns persist without input items (including the in_progress → failed update path). A follow-up request on the same conversation no longer sees the bad function_call_output. Tests cover both the store wrapper and the HTTP conversation repro from the issue.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@sgoel2be24-cyber Shikhar Goel (sgoel2be24-cyber) changed the title Python: skip Foundry session persist after a failed conversation turn Python: omit failed Foundry turns from conversation chat history Aug 14, 2026
Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
@github-actions

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/foundry_hosting/agent_framework_foundry_hosting
   _responses.py88212186%116–118, 234, 299–300, 314, 317–318, 329, 451, 465, 474, 551, 626, 641–644, 708, 758, 820, 832, 848–849, 854–857, 861–863, 868, 876, 879, 890, 935, 945, 956–958, 972–974, 977, 1011–1014, 1019, 1022, 1042–1043, 1139, 1210–1212, 1216, 1219–1222, 1226–1228, 1233–1237, 1241–1243, 1250–1251, 1254–1257, 1263, 1266–1272, 1282, 1288, 1292, 1316, 1325–1326, 1330, 1572, 1584, 2036–2037, 2041, 2086, 2088, 2090, 2092, 2096, 2104, 2107–2111, 2113, 2123, 2127, 2172, 2174, 2176–2179, 2187, 2189
TOTAL48125449690% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
9718 36 💤 0 ❌ 0 🔥 2m 43s ⏱️

Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
@moonbox3

Copy link
Copy Markdown
Contributor

/review

@eavanvalkenburg

Copy link
Copy Markdown
Member

Shikhar Goel (@sgoel2be24-cyber) please fix the merge conflict, and there is a update coming to the way we handle storage in #7997 I suggest updating this after that one merged

@sgoel2be24-cyber

Copy link
Copy Markdown
Author

Thanks Eduard van Valkenburg (@eavanvalkenburg). The merge conflict is fixed: rebased onto main (1a51fdd) and pushed 79aa6e3, including the prepared docstring and concrete-type cleanup. Foundry Hosting validation passes: 276 unit tests, 90% package coverage, Ruff, strict Pyright, and all five test type-checkers. The two cleanup threads are resolved; the streaming/storage thread remains open. I will revisit that design after #7997 merges, as suggested.

@sgoel2be24-cyber

Copy link
Copy Markdown
Author

Follow-up: #7997 merged while I was validating the earlier update. I have now rebased onto it (1f7f4b3) and pushed 8213cc6. Failed-input filtering is scoped to regular agents using AgentServer history; agent-owned history retains normal protocol storage and bypasses buffering. Added regression coverage for both modes and updated the resilient-store guard test for the new constructor validation order. All 294 Foundry Hosting unit tests pass, with 91% coverage; Ruff, strict Pyright, and all five test type-checkers pass. The docstring/type cleanup remains complete. The remaining default-mode buffering/API question is documented in the open review thread.

@moonbox3

Copy link
Copy Markdown
Contributor

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAF Automated Review — Iteration 1

Result: Findings reported
Scope: full PR (3 commit(s)): 3fb9d59225fd, 5b4281fae1fc, 8213cc685bc4
Model: gpt-5.6-sol-fast

Overview

The change correctly lets AgentServer resolve its configured store before wrapping it and adds end-to-end coverage that failed synchronous conversation turns are not replayed. The history-source guards and successful marker-consumption path are well tested. Two residual defects remain: failed-ID state is shared across tenant contexts and can survive persistence failures, while the provider wrapper also removes inputs from standalone failed responses that cannot affect conversation history.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
2 verified findings remained after source verification (1 high, 1 medium) across 1 file. Details are attached to the affected lines below.

Affected areas: python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py

Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
Signed-off-by: Shikhar Goel <223222024+sgoel2be24-cyber@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Foundry Hosting - User messages added to conversation chat history for failed requests

5 participants